package com.itfreer.ui.photoview;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.itfreer.core.file.FileMeta;
import com.itfreer.ui.R;
import com.itfreer.ui.activity.BaseActivity;

/**
 * 图片列表查看器
 */
public class PhotoListActivity extends BaseActivity {

    private PhotoViewPager gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_img_list);
        gridView = (PhotoViewPager)this.findViewById(R.id.main_gridview);
        gridView.setActivity(this);

        // 获取图片
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        FileMeta[] fileMetas = (FileMeta[]) bundle.get("fileMetas");// 获取到图片
        gridView.bindData(fileMetas, false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            onBack(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onBack(View view) {
        Intent intent = new Intent();
        FileMeta[] fms = new FileMeta[gridView.getFileMetas().size()];
        gridView.getFileMetas().toArray(fms);
        intent.putExtra("fileMetas", fms);
        setResult(RESULT_OK, intent);
        this.finish();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        gridView.onActivityResult(requestCode, resultCode, data);
    }
}
